package com.zhengqing.wxmp.util.wchat;


import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.LinkedList;
import java.util.List;

/**
 * @ClassName WchatServiceImpl
 * @Description TODO
 * @Author Des1gn
 * @Date 2022/9/27 14:25
 * @Version 1.0
 **/
@Service
@Slf4j
public class WchatServiceImpl implements WchatService {

    private final static String Appid = "wxb5e4c8c5300e321b";
    private final static String Appsecret = "30767181dada59c26cc9257787f1a3d2";

    @Override
    public AppLoginResponse appLogin(String code) {
        String url = String.format(WchatEnums.APP_LOGIN.getUrl(),Appid,Appsecret,code);
        String responseStr = HttpUtil.get(url);
        if (StringUtils.isEmpty(responseStr)){
            return null;
        }
        AppLoginResponse response = JSONObject.parseObject(responseStr,AppLoginResponse.class);
        if (!valiteSuccess(response)){
            log.error("小程序登录失败--{}", JSONObject.toJSONString(response));
            return null;
        }
        return response;
    }

    @Override
    public AccessTokenResponse accessToken(String grantType) {
        List<String> params = new LinkedList<>();
        params.add(grantType);
        params.add(Appid);
        params.add(Appsecret);
        String url = String.format(WchatEnums.GET_ACCESSTOKEN.getUrl(),grantType,Appid,Appsecret);
        String responseStr = HttpUtil.get(url);
        if (StringUtils.isEmpty(responseStr)){
            return null;
        }
        AccessTokenResponse response = JSONObject.parseObject(responseStr,AccessTokenResponse.class);
        if (!valiteSuccess(response)){
            log.error("ACCESS_TOKEN获取失败--{}", JSONObject.toJSONString(response));
            return null;
        }
        return response;
    }

    @Override
    public PhoneResponse phone(String accessToken, String code) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",code);
        String url = String.format(WchatEnums.GET_ACCESSTOKEN.getUrl(),accessToken);
        String responseStr = HttpUtil.post(url,jsonObject.toJSONString());
        if (StringUtils.isEmpty(responseStr)){
            return null;
        }
        PhoneResponse response = JSONObject.parseObject(responseStr,PhoneResponse.class);
        if (!valiteSuccess(response)){
            log.error("PHONE获取失败--{}", JSONObject.toJSONString(response));
            return null;
        }
        return response;
    }

    @Override
    public WchatBaseResponse sendTemplateMessage(String accessToken, SendTemplateMsgRequest request) {
        String url = String.format(WchatEnums.SEND_TEMPLATE_MSG.getUrl(),accessToken);
        String responseStr = HttpUtil.post(url, JSONObject.toJSONString(request));
        if (StringUtils.isEmpty(responseStr)){
            return null;
        }
        WchatBaseResponse response = JSONObject.parseObject(responseStr,WchatBaseResponse.class);
        if (!valiteSuccess(response)){
            log.error("模板消息发送失败--{}", JSONObject.toJSONString(response));
            return null;
        }
        return response;
    }

    @Override
    public WchatBaseResponse sendSubscribeMessage(String accessToken, SendSubscribeMsgRequest sendSubscribeMsgRequest) {
        String url = String.format(WchatEnums.SUBSCRIBE_MESG.getUrl(),accessToken);
        String responseStr = HttpUtil.post(url, JSONObject.toJSONString(sendSubscribeMsgRequest));
        if (StringUtils.isEmpty(responseStr)){
            return null;
        }
        WchatBaseResponse response = JSONObject.parseObject(responseStr,IdCardResponse.class);
        if (!valiteSuccess(response)){
            log.error("发送订阅消息失败--{}", JSONObject.toJSONString(response));
            return null;
        }
        return response;
    }

    @Override
    public IdCardResponse idCardInfo(String accessToken, String imgUrl) {
        String url = String.format(WchatEnums.GET_IDCARDINFO.getUrl(),imgUrl,accessToken);
        String responseStr = HttpUtil.post(url,"{}");
        if (StringUtils.isEmpty(responseStr)){
            return null;
        }
        IdCardResponse response = JSONObject.parseObject(responseStr,IdCardResponse.class);
        if (!valiteSuccess(response)){
            log.error("身份证信息获取失败--{}", JSONObject.toJSONString(response));
            return null;
        }
        return response;
    }

    private boolean valiteSuccess(WchatBaseResponse wchatBaseResponse){
        if (wchatBaseResponse.getErrcode() == null || wchatBaseResponse.getErrcode().equals(0)){
            return true;
        }
        return false;
    }
}
